<?php
namespace app\common\controller;

use think\Db;

class Pay
{
    //正式版配置
    private $gateway;
    private $appid;
    private $rsaPrivateKey;
    private $alipayrsaPublicKey;
    private $NotifyUrl;
    function __construct(){
        $this->gateway = config('alipay.gateway');
        $this->appid = config('alipay.appid');
        $this->rsaPrivateKey = config('alipay.rsaPrivateKey');
        $this->alipayrsaPublicKey = config('alipay.alipayrsaPublicKey');
        $this->NotifyUrl = config('alipay.NotifyUrl');
    }
    /**
     * 生成支付宝订单
     *
     * @ author: liuxiang
     * @ E-mail:18905833120@qq.com
     * @ date: 2017年7月31日
     * @access public|private|protected
     * @param  string      $subject    订单名称
     * @param  string      $body       订单描述
     * @param  int         $orderid    订单id
     * @param  string      $price      价格

     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function alipay_preorder($subject,$body,$orderid,$price)
    {
        include "static/alipay/aop/AopClient.php";
        include "static/alipay/aop/request/AlipayTradeAppPayRequest.php";
        /*支付宝支付接口*/
        $aop = new \AopClient();
        //**沙箱测试支付宝开始
        $aop->gatewayUrl = $this->gateway;
        //实际上线app id需真实的
        $aop->appId = $this->appid; //开发者appid
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $bizcontent = json_encode([
            'subject'=>$subject,
            'body'=>$body,
            'out_trade_no'=>$orderid,//此订单号为商户唯一订单号
            'total_amount'=>$price,//保留两位小数
            'product_code'=>'QUICK_MSECURITY_PAY'
        ]);
        //**沙箱测试支付宝结束
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();
        //支付宝回调
        $request->setNotifyUrl($this->NotifyUrl,true,true);
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        return $response;
        /*$arr['code']='success';
        $arr['data']=$response;
        echo json_encode($arr);exit;*/
    }

}
